<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2020/11/10
 * Time: 11:15
 */

namespace app\admin\controller;


use think\Db;
use think\Request;

class Painter extends Base
{
    /**
     * 首页展示
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax ()){
            $start_time = input('start_time');
            $end_time = input('end_time');
            $keyword = input('keyword');
            
            $where['apply_type'] = 1;
            $where['apply_status'] = 1;
            if(isset($keyword) && $keyword != ''){
                $where['apply_user_name'] = ['like', '%' . $keyword . '%'];
            }
            // 时间
            if(isset($start_time) && $start_time != '' && isset($end_time) && $end_time != ''){
                $where['apply_time'] = ['between', [$start_time, $end_time]];
            }
            if(isset($start_time) && $start_time != '' && isset($end_time) && $end_time == ''){
                $where['apply_time'] = ['>', $start_time];
            }
            if(isset($start_time) && $start_time == '' && isset($end_time) && $end_time != ''){
                $where['apply_time'] = ['<', $end_time];
            }
            
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('painter_apply')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::name('painter_apply')
                ->field('apply_id, apply_user_name, apply_user_phone, apply_user_email, apply_link, apply_explain, 
                apply_num, apply_time')
                ->where($where)
                ->page($Nowpage,$limits)
                ->order('apply_time desc')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    
    /**
     * 审核通过
     * @return \think\response\Json
     */
    public function applyAllow(){
        $apply_id = input('id');
        $apply = Db::table('up_painter_apply')->where('apply_id', $apply_id)->field('apply_user_id')->find();
        Db::startTrans();
        try{
            // 修改申请状态
            Db::table('up_painter_apply')
                ->where('apply_id', $apply_id)
                ->update([
                    'apply_check_time' => date('Y-m-d H:i:s'),
                    'apply_status' => 2,
                ]);
            // 修改用户状态
            Db::table('up_u_user')
                ->where('user_id', $apply['apply_user_id'])
                ->update([
                    'user_role' => 2,
                    'user_painter_time' => date('Y-m-d H:i:s')
                ]);
            // 如果没有店铺，插入店铺表
            $have = Db::table('up_shop')->where('shop_user_id', $apply['apply_user_id'])->find();
            if(!$have){
                Db::table('up_shop')->insert(['shop_user_id' => $apply['apply_user_id'], 'shop_time' => date('Y-m-d H:i:s')]);
            }
            Db::commit();
            return json(['code' => 200, 'msg' => '审核成功']);
        }catch(\Exception $e){
            Db::rollback();
            return json(['code' => 100, 'msg' => $e->getMessage()]);
        }
    }
    
    /**
     * 拒绝申请
     * @return mixed|\think\response\Json
     */
    public function applyRefuse(){
        $apply_id = input('apply_id');
        if(Request::instance()->isPost()){
            $apply_false_reason = input('apply_false_reason');
            $res = Db::table('up_painter_apply')
                ->where('apply_id', $apply_id)
                ->update([
                    'apply_check_time' => date('Y-m-d H:i:s'),
                    'apply_status' => 3,
                    'apply_false_reason' => $apply_false_reason
                ]);
            if($res){
                return json(['code' => 200, 'msg' => '审核成功']);
            }else{
                return json(['code' => 100, 'msg' => '审核失败']);
            }
        }
        $this->assign('apply_id', $apply_id);
        return $this->fetch('painter/applyrefuse');
    }
}